package exer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author tongchen
 * @create 2023-04-01 17:23
 */
@WebServlet("/json")
public class JsonTest extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求数据的编码集
        req.setCharacterEncoding("utf-8");
        //设置响应数据的格式
        resp.setContentType("application/json;charset=utf-8");
        //创建字节流
        byte[] bytes = new byte[req.getContentLength()];
        req.getInputStream().read(bytes);
        //通过bytes数组创建内容
        String s = new String(bytes,"utf-8");
        resp.getWriter().write(s);

    }
}
